<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- table表格区域 -->
      <el-table :data="goodsList" border stripe style="width: 100%">
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)"
          width="95px"
        ></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70px">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="140px">
          <template #default="{ row }">
            {{ row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template #default="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button
            ><el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delGoodById(row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getGoodsListApi, delGoodByIdApi } from '@/api/list.js'
export default {
  data () {
    return {
      // 分页查询参数
      queryInfo: {
        pagenum: 1,
        pagesize: 10,
        query: ''
      },
      total: 0, // 总数
      // 商品列表数据
      goodsList: []
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 获取商品数据列表
    async getGoodsList () {
      const { data } = await getGoodsListApi(this.queryInfo)
      // console.log(data)
      if (data.meta.status !== 200) return this.$message.error('获取失败!')
      this.$message.success('获取成功')
      this.goodsList = data.data.goods
      this.total = data.data.total
    },
    // 监听页面大小改变
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听页码改变
    handleCurrentChange (newPagenum) {
      this.queryInfo.pagenum = newPagenum
      this.getGoodsList()
    },
    // 删除商品
    async delGoodById (id) {
      try {
        await this.$confirm('确定删除该商品吗')
        await delGoodByIdApi(id)
        const newPage = Math.ceil((this.total - 1) / this.queryInfo.pagesize)
        if (newPage < this.queryInfo.pagenum) {
          this.queryInfo.pagenum = newPage
        }
        this.$message.success('删除成功')
        this.getGoodsList()
      } catch (error) {
        console.log(error)
        this.$message.info('取消了删除')
      }
    },
    // 添加商品
    goAddPage () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
</style>
